<%@page language="java" contentType="text/html;charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<title>用户列表</title>
	<meta charset="utf-8">
	<style>
        .w70 {width: 70px !important;}
        .table {
        	padding:10px;
        }
    </style>
    <%@include file="/common/table.jsp"%>
    <script type="text/javascript" src="${ctx }/bootstrap/js/plugins/treegrid/bootstrap.treegrid.js"></script>
</head>
<body>
	<div class="table">
	    <div id="toolbar">
	        <div class="form-inline" role="form">
	            <div class="form-group">
	                <span>姓名: </span>
	                <input name="f_like_vcName" class="form-control">
	            </div>
	            
	            <button type="submit" class="btn btn-default ok">查询</button>
	            <button type="submit" class="btn btn-info add">新增</button>
	            <button type="submit" class="btn btn-danger del">删除</button>
	            <button type="submit" class="btn btn-warning edit">修改</button>
	           	<button type="submit" class="btn btn-info addSub">新增子项目</button>
	        </div>
	    </div>
	    <table id="table"
	           data-toggle="table"
	           data-toolbar="#toolbar"
	           data-show-refresh="true"
	           data-show-toggle="true"
	           data-show-columns="true"
	           data-query-params="queryParams"
	           data-response-handler="responseHandler"
	           data-checkbox="true"
	           data-click-to-select="true"
	           data-unique-id="id"
	           data-url="${ctx }/sys/systorg/list.do">
	        <thead>
	        <tr>
	        	<th data-field="state" data-checkbox="true"></th>
	            <th data-field="vcName">名称</th>
	            <th data-field="vcEnname">英文简写</th>
	            <th data-field="ndataScope">范围</th>
	            <th data-field="norder">序号</th>
	        </tr>
	        </thead>
	    </table>
	</div>

<script>
	$("#table").bootstrapTable({
		treeId:'id',
		treeField:'vcName',
		sidePagination: 'client',
        pagination: false,
		clickToSelect: true,
		expandAll: false,
		treeView: true,
		contentType : "application/x-www-form-urlencoded"
	});
	
	var $table = $('#table'), $ok = $('.btn.btn-default.ok')
		, $add = $('.btn.btn-info.add'), $del = $('.btn.btn-danger.del')
		, $edit = $('.btn.btn-warning.edit')
		, $addSub = $('.btn.btn-info.addSub')
	;

	$(function() {
		$ok.click(function() {
			$table.bootstrapTable('refresh');
		});
		$add.bind('click', add);
		$del.bind('click', del);
		$edit.bind('click', edit);
		$addSub.bind('click', addSub);
	});

	function queryParams() {
		var params = {};
		$('#toolbar').find('input[name]').each(function() {
			params[$(this).attr('name')] = $(this).val();
		});
		return params;
	}

	function responseHandler(res) {
		return res;
	}
	
	function add(){
		layer.open({
			type: 2,
			title: '添加人员',
			shadeClose: true,
			maxmin: true,
			closeBtn: 1,
			shade: 0.8,
			area: ['40%', '90%'],
			content: '${ctx }/sys/systorg/viewinput.do', //iframe的url
			btn: ['保存', '关闭'],
			yes: function(index, $lay, xx){
				var formData = $lay.find('iframe')[0].contentWindow.formData();
				layer.closeAll();
				$.post('${ctx }/sys/systorg/save.do', formData, function(data){
					layer.msg('保存成功', {timeout:1000});
					var jsonData = JSON.parse(data);
					var arr = [];
					arr.push(jsonData);
					$table.bootstrapTable('append', arr);
				});
			},
			btn2: function(){
				layer.closeAll();
			}
		});
	}
	
	function addSub(){
		var id = $table.bootstrapTable('getSelections')[0].id;
		layer.open({
			type: 2,
			title: '添加人员',
			shadeClose: true,
			maxmin: true,
			closeBtn: 1,
			shade: 0.8,
			area: ['40%', '90%'],
			content: '${ctx }/sys/systorg/viewinputSub.do?id=' + id, //iframe的url
			btn: ['保存', '关闭'],
			yes: function(index, $lay, xx){
				var formData = $lay.find('iframe')[0].contentWindow.formData();
				layer.closeAll();
				$.post('${ctx }/sys/systorg/save.do', formData, function(data){
					layer.msg('保存成功', {timeout:1000});
					var jsonData = JSON.parse(data);
					var arr = [];
					arr.push(jsonData);
					$table.bootstrapTable('append', arr);
				});
			},
			btn2: function(){
				layer.closeAll();
			}
		});
	}
	
	function edit(){
		var id = $table.bootstrapTable('getSelections')[0].id;
		layer.open({
			type: 2,
			title: '修改人员',
			shadeClose: true,
			maxmin: true,
			closeBtn: 1,
			shade: 0.8,
			area: ['40%', '90%'],
			content: '${ctx }/sys/systorg/viewedit.do?id=' + id, //iframe的url
			btn: ['保存', '关闭'],
			yes: function(index, $lay, xx){
				var formData = $lay.find('iframe')[0].contentWindow.formData();
				layer.closeAll();
				$.post('${ctx }/sys/systorg/save.do', formData, function(data){
					layer.msg('修改成功', {timeout:1000});
					var jsonData = JSON.parse(data);
					$table.bootstrapTable('updateByUniqueId', {id: id, row: jsonData});
				});
			},
			btn2: function(){
				layer.closeAll();
			}
		});
	}
	
	function del(){
		var ids = $.map($table.bootstrapTable('getSelections'), function (row) {
            return row.id;
        });
		$.post('${ctx }/sys/systorg/delete.do', {ids: ids.join(',')}, function(){
			layer.msg('删除成功', {timeout:1000});
            $table.bootstrapTable('remove', {
            	field: 'id',
            	values: ids
            });
		});
	}
</script>
</body>
</html>